//
//  BIDApplyListViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDApplyListViewController.h"
#import "BIDApplyDetailViewController.h"
#import "BIDAddApplyViewController.h"
#import "BIDApplyCell.h"

//请示列表
static NSString *listURL = @"requestAndroid!gainRequestSendList.action";
//删除
static NSString *deleteURL = @"requestAndroid!deleteRequests.action";

@interface BIDApplyListViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    BOOL _isRegister;
}
@end

@implementation BIDApplyListViewController
@synthesize isApply;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addApply)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], listURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld&userId=%@&type=%@", self.curPage, self.countPerPage, [AppDelegate getUserId], isApply?@0:@1];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            [self.dataSourceArr addObjectsFromArray:arr];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
        }
        [self.myTableView reloadData];
    }];
}

- (void)addApply
{
    BIDAddApplyViewController *vc = [[BIDAddApplyViewController alloc] initWithNibName:@"BIDAddApplyViewController" bundle:nil];
    vc.title = isApply?@"请示管理":@"报告管理";
    vc.isApply = self.isApply;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self setNavigationControllerAttribute:nav];
    [self presentViewController:nav animated:YES completion:^{}];
}

- (void)deleteApplyAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    NSString *applyID = dic[@"id"];
    NSString *strRequestURL = @"";
    strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@&userId=%@", applyID, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary* responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            //删除成功
            [self.dataSourceArr removeObjectAtIndex:row];
            [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDApplyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDApplyCell *applyCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    applyCell.titleLabel.text = dic[@"title"];
    applyCell.nameLabel.text = [[NSString alloc] initWithFormat:@"%@ %@", dic[@"createUserName"], dic[@"createTime"]];
    applyCell.remarksLabel.text = dic[@"isFinish"];
    cell = applyCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BIDApplyDetailViewController *vc = [[BIDApplyDetailViewController alloc] initWithNibName:@"BIDApplyDetailViewController" bundle:nil];
    vc.applyID = self.dataSourceArr[indexPath.row][@"id"];
    vc.isApply = self.isApply;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 93.f;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //删除某条会议
        [self deleteApplyAtIndexPath:indexPath];
    }
}

@end
